package com.lightChat.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author sxt
 * @create 2019-11-13 20:15
 */
public class NettyTest {


    public static void main(String[] args) throws Exception {
        //创建主从线程组
        NioEventLoopGroup mainLoopGroup = new NioEventLoopGroup();
        NioEventLoopGroup subLoopGroup = new NioEventLoopGroup();

       try {
           //netty服务启动对象
           ServerBootstrap serverBootstrap = new ServerBootstrap();
           serverBootstrap.group(mainLoopGroup,subLoopGroup); //加入主从线程池
           serverBootstrap.channel(NioServerSocketChannel.class); //设置nio双向通道
           serverBootstrap.childHandler(new HelloChannelInitializer());  //子处理器

           //启动服务，设定端口号为8081，以同步的方式启动
           ChannelFuture channelFuture = serverBootstrap.bind(8081).sync();

           //监听channel的关闭，以同步的方式进行
           Channel channel = channelFuture.channel();
           channel.closeFuture().sync();
       }finally {

        //服务关闭操作
        mainLoopGroup.shutdownGracefully();
        subLoopGroup.shutdownGracefully();
       }



    }

}
